package com.xrh.xdemo.service.controllers;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xrh.core.bean.CommonResult;
import com.xrh.core.enums.ResultCodeEnum;
import com.xrh.core.exceptions.BusinessException;
import com.xrh.xdemo.api.dto.extend.SysUserDetailDTO;
import com.xrh.xdemo.api.request.common.LoginRequest;
import com.xrh.xdemo.service.annotations.OperateTrace;
import com.xrh.xdemo.service.service.LoginService;
import com.xrh.xdemo.service.service.SysUserService;
import com.xrh.xdemo.service.utils.LoginUtils;
import com.xrh.xdemo.service.utils.LoginValidCodeUtils;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.generator.RandomGenerator;
import cn.hutool.core.util.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags="登录API")
@RestController
@RequestMapping("login")
@Slf4j
public class LoginController {

	@Resource
	LoginService loginService;
	
	@Resource
	SysUserService sysUserService;
	
	@Autowired
	private HttpServletResponse response;
	
	@Autowired
	private HttpServletRequest request;
	
	/**
	 * @function 登录
	 * @author 肖荣辉
	 * @date 2023年12月8日
	*/
   	@ApiOperation("登录")
   	@PostMapping("/login")
   	@OperateTrace(operateType = "登录系统")
   	public  CommonResult<String> login(@RequestBody @Valid LoginRequest loginRequest){
   		
   		try {
   			
   			String str = loginService.login(loginRequest);
   			
   			return CommonResult.success(str);
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("login 发生错误 ==> " , e);
   			return CommonResult.failed(e.getMessage());
   		}
   		
   	}
   	
   	/**
   	 * @function 获取登录信息
   	 * @author 肖荣辉
   	 * @date 2023年12月8日
   	 */
   	@ApiOperation("获取登录信息")
   	@PostMapping("/getLoginInfo")
   	public  CommonResult<SysUserDetailDTO> getLoginInfo(){
   		
   		try {
   			
   			return CommonResult.success(LoginUtils.getUser());
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("getLoginInfo 发生错误 ==> " , e);
   			return CommonResult.failed(e.getMessage());
   		}
   		
   	}
	
    /**
   	 * @function 登出
   	 * @author 肖荣辉
   	 * @date 2022-09-05 13:38:07
   	*/
   	@ApiOperation("登出")
   	@PostMapping("/logout")
   	@OperateTrace(operateType = "退出系统")
   	public  CommonResult<String> logout(){
   		
   		try {
   		
   			return CommonResult.success();
   			
   		} catch (BusinessException e) {
   			return CommonResult.failed(e.getCode() , e.getMsg());
   		} catch (Exception e) {
   			log.error("logout 发生错误 ==> " , e);
   			return CommonResult.failed(e.getMessage());
   		}
   		
   	}
   	
   	
	/**
	 * @function 是否需要展示登录验证码
	 * @author 肖荣辉
	 * @date 2023年06月25日
	 */
	@ApiOperation("是否需要展示登录验证码")
	@PostMapping("/needShowLoginValidCode")
	public CommonResult<Boolean> needShowLoginValidCode() {
		
		try {
			
			return CommonResult.success(LoginValidCodeUtils.needShowLoginValidCode()) ;
					
		} catch (Exception e) {
			log.error("needShowLoginValidCode发生错误==>" , e);
			return CommonResult.failed(ResultCodeEnum.SYSTEM_FAILED);
		}
		
	}
   	
   	/**
	 * @function 下载登录验证码
	 * @author 肖荣辉
	 * @date 2020年11月3日
	*/
	@ApiOperation("下载登录验证码")
	@GetMapping("/downLoadLoginErrorValidCode")
	public void downLoadLoginErrorValidCode() {
		
		try {
			
			//数字和字母
		    RandomGenerator randomGenerator = new RandomGenerator(RandomUtil.BASE_CHAR_NUMBER , 4);
		    
	        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
	        
	        lineCaptcha.setGenerator(randomGenerator);
	        
	        // 重新生成code
	        lineCaptcha.createCode();
	        
	        //设置登录验证码
	        LoginValidCodeUtils.setErrorValidCode(lineCaptcha.getCode());
	        
	        lineCaptcha.write(response.getOutputStream());
	        
	        // 关闭流
	        response.getOutputStream().close();
			
		}catch (Exception e) {
			log.error("downLoadLoginErrorValidCode发生错误==>" , e);
		} 
		
	}
	
	
}
